//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "InterestRateVolSurface.h"
using namespace Cephei::QL::Experimental::Volatility;
#include <gen/QL/Indexes/InterestRateIndex.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Experimental/Volatility/BlackVolSurface.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (boost::shared_ptr<QuantLib::InterestRateVolSurface>& childNative, Object^ owner) : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
#ifdef HANDLE
	_phInterestRateVolSurface = NULL;
#endif
	_ppInterestRateVolSurface = &childNative;
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppInterestRateVolSurface));
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (QuantLib::InterestRateVolSurface& childNative, Object^ owner) : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
#ifdef HANDLE
	_phInterestRateVolSurface = NULL;
#endif
	_ppInterestRateVolSurface = new boost::shared_ptr<QuantLib::InterestRateVolSurface> (&childNative);
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppInterestRateVolSurface));
    _InterestRateVolSurfaceOwner = owner;
    _BlackVolSurfaceOwner = owner;
}

Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (CInterestRateVolSurface^ copy) : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
#ifdef HANDLE
	_phInterestRateVolSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInterestRateVolSurface = new boost::shared_ptr<QuantLib::InterestRateVolSurface> (copy->GetShared());
        _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppInterestRateVolSurface));
    }
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (System::Type^ t) : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
#ifdef HANDLE
	_phInterestRateVolSurface = NULL;
#endif
	if (!t->IsSubclassOf(CInterestRateVolSurface::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (QuantLib::Handle<QuantLib::InterestRateVolSurface>& childNative, Object^ owner)  : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
	_phInterestRateVolSurface = &childNative;
	_ppInterestRateVolSurface = &static_cast<boost::shared_ptr<QuantLib::InterestRateVolSurface>>(childNative.currentLink());
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppInterestRateVolSurface));
    _InterestRateVolSurfaceOwner = owner;
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (QuantLib::Handle<QuantLib::InterestRateVolSurface> childNative)  : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
	_phInterestRateVolSurface = &childNative;
	_ppInterestRateVolSurface = &static_cast<boost::shared_ptr<QuantLib::InterestRateVolSurface>>(childNative.currentLink());
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppInterestRateVolSurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::CInterestRateVolSurface (QuantLib::InterestRateVolSurface childNative)  : CBlackVolSurface(CInterestRateVolSurface::typeid)
{
#ifdef HANDLE
	_phInterestRateVolSurface = NULL;
#endif
	_ppInterestRateVolSurface = new boost::shared_ptr<QuantLib::InterestRateVolSurface> (new QuantLib::InterestRateVolSurface (childNative));
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppInterestRateVolSurface));
}
#endif

Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::~CInterestRateVolSurface ()
{
    if (_ppInterestRateVolSurface != NULL)
    {
	    delete _ppInterestRateVolSurface;
        _ppInterestRateVolSurface = NULL;
    }
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::!CInterestRateVolSurface ()
{
    if (_ppInterestRateVolSurface != NULL)
    {
	    delete _ppInterestRateVolSurface;
    }
}
QuantLib::InterestRateVolSurface& Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::GetReference ()
{
    if (_ppInterestRateVolSurface == NULL) throw gcnew NativeNullException ();
	return **_ppInterestRateVolSurface;
}
boost::shared_ptr<QuantLib::InterestRateVolSurface>& Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::GetShared ()
{
    if (_ppInterestRateVolSurface == NULL) throw gcnew NativeNullException ();
	return *_ppInterestRateVolSurface;
}
QuantLib::InterestRateVolSurface* Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::GetPointer ()
{
    if (_ppInterestRateVolSurface == NULL) throw gcnew NativeNullException ();
	return &**_ppInterestRateVolSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InterestRateVolSurface>& Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::GetHandle ()
{
	if (_phInterestRateVolSurface == NULL)
	{
		_phInterestRateVolSurface = new Handle<QuantLib::InterestRateVolSurface> (*_ppInterestRateVolSurface);
	}
	return *_phInterestRateVolSurface;
}
#endif
bool Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::HasNative () 
{
	return (_ppInterestRateVolSurface != NULL);
}

Cephei::QL::Indexes::IInterestRateIndex^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::Index::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::InterestRateIndex>& _rv = (boost::shared_ptr<QuantLib::InterestRateIndex>&)(*_ppInterestRateVolSurface)->index ( );   
        Cephei::QL::Indexes::CInterestRateIndex^ _nrv = gcnew Cephei::QL::Indexes::CInterestRateIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Volatility::CInterestRateVolSurface::OptionDateFromTenor (Cephei::QL::Times::IPeriod^ p)
{
    CPeriod^ _Cp;
    try
    {
        _Cp = safe_cast<CPeriod^> (p);
        _Cp->Lock();
        QuantLib::Period& _p = static_cast<QuantLib::Period&> (_Cp->GetReference ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppInterestRateVolSurface)->optionDateFromTenor ( _p );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
    }
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CInterestRateVolSurfaceVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::~CInterestRateVolSurfaceVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::!CInterestRateVolSurfaceVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CInterestRateVolSurfaceVector (std::vector<QuantLib::InterestRateVolSurface>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CInterestRateVolSurfaceVector (std::vector<QuantLib::InterestRateVolSurface> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::InterestRateVolSurface> (vec);
}
#endif
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CInterestRateVolSurfaceVector (std::vector<boost::shared_ptr<QuantLib::InterestRateVolSurface>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CInterestRateVolSurfaceVector (std::vector<QuantLib::Handle<QuantLib::InterestRateVolSurface>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw gcnew RangeException ();
		QuantLib::InterestRateVolSurface& native = (*_pVValue)[index];
		return gcnew CInterestRateVolSurface (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw gcnew RangeException ();
		QuantLib::InterestRateVolSurface& native = *(*_pVBoost)[index];
		return gcnew CInterestRateVolSurface (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw gcnew RangeException ();
		QuantLib::InterestRateVolSurface& native = *((*_pVHand)[index].currentLink ());
		return gcnew CInterestRateVolSurface (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::default::set (int index, Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CInterestRateVolSurface^>(value)->GetReference ());
		}
        else
        {
            QuantLib::InterestRateVolSurface& c = (*_pVValue)[index];
            c = dynamic_cast<CInterestRateVolSurface^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CInterestRateVolSurface^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::InterestRateVolSurface>& c = (*_pVBoost)[index];
		c = dynamic_cast<CInterestRateVolSurface^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::InterestRateVolSurface> ());
		}
		QuantLib::Handle<QuantLib::InterestRateVolSurface>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::InterestRateVolSurface>& b = static_cast<boost::shared_ptr<QuantLib::InterestRateVolSurface>>(c.currentLink());
		b = dynamic_cast<CInterestRateVolSurface^>(value)->GetShared ();
	}
#endif
}
Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CItem::get (int index) 
{
	return CellBuilder::Instance->Create<IInterestRateVolSurface^> (dynamic_cast<IInterestRateVolSurface^>(default[index]));
}

void Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::CItem::set (int index, Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ value) 
{
	default[index] = safe_cast<IInterestRateVolSurface^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::InterestRateVolSurface>& Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::InterestRateVolSurface>>& Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::InterestRateVolSurface>>& Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Experimental::Volatility::CInterestRateVolSurfaceVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::InterestRateVolSurface> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::InterestRateVolSurface>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::InterestRateVolSurface>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of InterestRateVolSurface that can be passed into other function </summary> */
IVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurface_Factory::CreateVector ()
 {
	 return gcnew CoVector<IInterestRateVolSurface^> (gcnew CInterestRateVolSurfaceVector ());
 }
IVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurface_Factory::CreateVector (System::Collections::Generic::IEnumerable<IInterestRateVolSurface^>^ source)
 {
	 return gcnew CoVector<IInterestRateVolSurface^> (source, gcnew CInterestRateVolSurfaceVector ());
 }
/* <summary>Create a single dimension array of InterestRateVolSurface with change events</summary> */
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^>^>^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurface_Factory::CreateCellVector ()
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^> (gcnew CInterestRateVolSurfaceVector ()));
 }
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^>^>^ Cephei::QL::Experimental::Volatility::CInterestRateVolSurface_Factory::CreateCellVector (System::Collections::Generic::IEnumerable<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^>^ source)
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^> (source, gcnew CInterestRateVolSurfaceVector ()));
 }

